home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / GetPPPStatus / sources / AEHelpers.c next >
Encoding:
Text File  |  1998-02-06  |  13.4 KB  |  531 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Functions to help you when you are building and sending Apple events.
  5. **
  6. **    by Andy Bachorski, Apple Developer Technical Support
  7. **
  8. **    File:        AEHelpers.c
  9. **
  10. **    Version:    0.3.5
  11. **
  12. **    Copyright © 1996 Apple Computer, Inc.
  13. **    All rights reserved.
  14. **
  15. **    You may incorporate this sample code into your applications without
  16. **    restriction, though the sample code has been provided "AS IS" and the
  17. **    responsibility for its operation is 100% yours.  However, what you are
  18. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  19. **    after having made changes. If you're going to re-distribute the source,
  20. **    we require that you make it clear in the source that the code was
  21. **    descended from Apple Sample Code, but that you've made changes.
  22. */
  23.  
  24. //    Constant used to #undef PASCAL when not compiling a library
  25. #define    COMPILING_MORE_FINDER_EVENTS    true
  26.  
  27. //    Prefix file
  28. #include "Prefix.h"
  29.  
  30. //    System includes
  31. #include <AERegistry.h>
  32. #include <AEObjects.h>
  33. #include <AEPackObject.h>
  34. #include <Aliases.h>
  35. #include <Gestalt.h>
  36. #include <Icons.h>
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40.  
  41. //    MoreFinderEvents includes
  42. #include "FinderRegistry.h"
  43. #include "MoreFinderEvents.h"
  44. #include "TrapUtils.h"
  45.  
  46. //    Export symbols in this header for shared libraries
  47. #pragma export on
  48. #include "AEHelpers.h"
  49. #pragma export off
  50.  
  51.  
  52. // *****************************************************************************
  53. //    Private Prototypes
  54. // *****************************************************************************
  55.  
  56. pascal    OSErr    MyIconAction (ResType theIconType,
  57.                            Handle *theIcon,
  58.                            void *myDataPtr);
  59.                            
  60. /*
  61.     Used by AEHMakeIconFamilyRecord, passed to ForEachIconDo as the IconAction
  62.     function. Puts each icon in an icon suite into the descriptor record passed
  63.     in the myDataPtr parameter.
  64. */
  65.  
  66. // ************************************************************************************************
  67.  
  68. PASCAL    OSErr    FindProcessBySignature( const OSType targetType,
  69.                                         const OSType targetCreator,
  70.                                         ProcessSerialNumberPtr psnPtr )
  71. {
  72.     OSErr        anErr = noErr;
  73.     Boolean        foundTheProcess = false;
  74.     
  75.     ProcessInfoRec    infoRec;
  76.     
  77.     infoRec.processInfoLength = sizeof( ProcessInfoRec );
  78.     infoRec.processName = nil;
  79.     infoRec.processLocation = nil;
  80.     infoRec.processAppSpec = nil;
  81.     
  82.     psnPtr->lowLongOfPSN = kNoProcess;
  83.     psnPtr->highLongOfPSN = kNoProcess;
  84.  
  85.     while ( !foundTheProcess )
  86.     {
  87.         anErr = GetNextProcess( psnPtr );
  88.         if ( anErr == noErr )
  89.         {
  90.             anErr = GetProcessInformation( psnPtr, &infoRec );
  91.             if ( ( anErr == noErr )
  92.                  && ( infoRec.processType == targetType )
  93.                  && ( infoRec.processSignature == targetCreator ) )
  94.             {
  95.                 foundTheProcess = true;
  96.             }
  97.         }
  98.     }
  99.     
  100.     return anErr;
  101.  
  102. }//end FindProcessBySignature
  103.  
  104. // ************************************************************************************************
  105.  
  106. PASCAL    OSErr    AEHMakeAppleEventSignatureTarget( const OSType targetType,
  107.                                                   const OSType targetCreator,
  108.                                                   const AEEventClass eventClass,
  109.                                                   const AEEventID eventID,
  110.                                                         AppleEvent *theEvent )
  111. {
  112.     OSErr    anErr = noErr;
  113.     
  114.     ProcessSerialNumber        psn = { kNoProcess, kNoProcess };
  115.     
  116.     anErr = FindProcessBySignature( targetType, targetCreator, &psn );
  117.     if ( anErr == noErr )
  118.     {
  119.         anErr = AEHMakeEventProcessTarget( &psn, eventClass, eventID, theEvent );
  120.     }
  121.     return anErr;
  122. }//end AEHMakeAppleEventSignatureTarget
  123.  
  124. // ************************************************************************************************
  125.  
  126. PASCAL    OSErr    AEHMakeEventProcessTarget( const ProcessSerialNumberPtr psnPtr,
  127.                                            const AEEventClass eventClass,
  128.                                            const AEEventID eventID,
  129.                                                  AppleEvent *theEvent )
  130. {
  131.     OSErr    anErr = noErr;
  132.     AEDesc    targetAppDesc = { typeNull, nil };
  133.     
  134.     anErr = AECreateDesc (typeProcessSerialNumber, psnPtr, sizeof( ProcessSerialNumber ), &targetAppDesc);
  135.  
  136.     if ( anErr == noErr )
  137.     {
  138.         anErr = AECreateAppleEvent( eventClass, eventID, &targetAppDesc,
  139.                                     kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  140.     }
  141.     
  142.     AEDisposeDesc( &targetAppDesc );
  143.     
  144.     return anErr;
  145. }//end AEHMakeEventProcessTarget
  146.  
  147. // ************************************************************************************************
  148.  
  149. PASCAL    OSErr    AEHMakeEventTargetID( const TargetID *targetIDPtr,
  150.                                       const AEEventClass eventClass,
  151.                                       const AEEventID eventID,
  152.                                             AppleEvent *theEvent )
  153. {
  154.     OSErr    anErr = noErr;
  155.     AEDesc    targetAppDesc = { typeNull, nil };
  156.     
  157.     anErr = AECreateDesc (typeTargetID, targetIDPtr, sizeof( TargetID ), &targetAppDesc);
  158.  
  159.     if ( anErr == noErr )
  160.     {
  161.         anErr = AECreateAppleEvent( eventClass, eventID, &targetAppDesc,
  162.                                     kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  163.     }
  164.     
  165.     AEDisposeDesc( &targetAppDesc );
  166.     
  167.     return anErr;
  168. }//end AEHMakeEventProcessTarget
  169.  
  170. // ************************************************************************************************
  171.  
  172. PASCAL    OSErr    AEHMakeAliasDescFromFSSpec( const FSSpecPtr fssPtr,
  173.                                             AEDesc *aliasDesc )
  174. {
  175.     OSErr            anErr = noErr;
  176.     AliasHandle        aliasHandle;
  177.     
  178.     NewAlias( nil, fssPtr, &aliasHandle);
  179.  
  180.     if ( aliasHandle == nil )
  181.     {
  182.         anErr = paramErr;
  183.     }
  184.     else
  185.     {
  186.         anErr = AEHMakeAliasDesc( aliasHandle, aliasDesc );
  187.         DisposeHandle( (Handle)aliasHandle );
  188.     }
  189.         
  190.     return anErr;
  191. }//end MakeAliasObject
  192.  
  193. // ************************************************************************************************
  194.  
  195. PASCAL    OSErr    AEHMakeAliasDesc( const AliasHandle aliasHandle,
  196.                                         AEDesc *aliasDesc )
  197. {
  198.     OSErr    anErr = noErr;
  199.     
  200.     char    handleState = HGetState( (Handle)aliasHandle );
  201.     HLock( (Handle)aliasHandle );
  202.     
  203.     anErr = AECreateDesc( typeAlias, *aliasHandle, GetHandleSize( (Handle)aliasHandle ), aliasDesc );
  204.     
  205.     HSetState( (Handle)aliasHandle, handleState );
  206.     
  207.     return anErr;
  208. }//end MakeAliasObject
  209.  
  210. // ************************************************************************************************
  211.  
  212. PASCAL    OSErr    AEHMakeAliasObjectFromFSSpec( const FSSpecPtr fssPtr,
  213.                                                     AEDesc *containerObj,
  214.                                                     AEDesc *aliasObject )
  215. {
  216.     OSErr            anErr = noErr;
  217.     AliasHandle        aliasHandle;
  218.     
  219.     anErr = NewAlias( nil, fssPtr, &aliasHandle);
  220.     if ( aliasHandle == nil )
  221.     {
  222.         anErr = paramErr;
  223.     }
  224.     
  225.     if ( anErr == noErr )
  226.     {
  227.         anErr = AEHMakeAliasObject( aliasHandle, containerObj, aliasObject );
  228.     }
  229.     
  230.     DisposeHandle( (Handle)aliasHandle );
  231.     
  232.     return anErr;
  233. }//end MakeAliasObject
  234.  
  235. // ************************************************************************************************
  236.  
  237. PASCAL    OSErr    AEHMakeAliasObject( const AliasHandle aliasHandle,
  238.                                           AEDesc *containerObj,
  239.                                           AEDesc *aliasObject )
  240. {
  241.     OSErr    anErr = noErr;
  242.     AEDesc    aliasDesc;
  243.  
  244.     char    handleState = HGetState( (Handle)aliasHandle );
  245.     HLock( (Handle)aliasHandle );
  246.     
  247.     anErr = AECreateDesc( typeAlias, *aliasHandle, GetHandleSize( (Handle)aliasHandle ), &aliasDesc );
  248.     HSetState( (Handle)aliasHandle, handleState );
  249.     
  250.     if ( anErr == noErr )
  251.     {
  252.         anErr = CreateObjSpecifier( typeAlias, containerObj, formAbsolutePosition,
  253.                                     &aliasDesc, true, aliasObject );
  254.         AEDisposeDesc( &aliasDesc );
  255.     }
  256.     
  257.     return anErr;
  258. }//end MakeAliasObject
  259.  
  260. // ************************************************************************************************
  261.  
  262. PASCAL    OSErr    AEHMakePropertyObject( const DescType propType,
  263.                                              AEDesc *containerObj,
  264.                                              AEDesc *propertyObj )
  265. {
  266.     OSErr    anErr = noErr;
  267.     AEDesc    propDesc;
  268.     
  269.     anErr = AECreateDesc( typeType, &propType, sizeof( propType ), &propDesc );
  270.     
  271.     if ( anErr == noErr )
  272.     {
  273.         anErr = CreateObjSpecifier( cProperty, containerObj, formPropertyID,
  274.                                     &propDesc, true, propertyObj );
  275.         AEDisposeDesc( &propDesc );
  276.     }
  277.     
  278.     return anErr;
  279. }//end MakePropertyObject
  280.  
  281. // ************************************************************************************************
  282.  
  283. PASCAL    OSErr    AEHMakeProcessObject( const ProcessSerialNumber *psnPtr,
  284.                                              AEDesc *containerObj,
  285.                                              AEDesc *propertyObj )
  286. {
  287.     OSErr    anErr = noErr;
  288.     AEDesc    psnDesc;
  289.     
  290.     anErr = AECreateDesc( typeProcessSerialNumber, psnPtr, sizeof( ProcessSerialNumber ), &psnDesc );
  291.     
  292.     if ( anErr == noErr )
  293.     {
  294.         anErr = CreateObjSpecifier( cProperty, containerObj, formPropertyID,
  295.                                     &psnDesc, true, propertyObj );
  296.         AEDisposeDesc( &psnDesc );
  297.     }
  298.     
  299.     return anErr;
  300. }//end MakePropertyObject
  301.  
  302. // ************************************************************************************************
  303.  
  304. PASCAL    OSErr    AEHMakeSelectionObject( const DescType selection,
  305.                                               AEDesc *containerObj,
  306.                                               AEDesc *selectionObject )
  307. {
  308.     OSErr    anErr = noErr;
  309.     
  310.     AEDesc    selectionDesc = { typeNull, nil };
  311.     
  312.     anErr = AECreateDesc( typeAbsoluteOrdinal, &selection, sizeof( selection ), &selectionDesc );
  313.  
  314.     if ( anErr == noErr )
  315.     {
  316.         anErr = CreateObjSpecifier( cObject, containerObj, formAbsolutePosition,
  317.                                     &selectionDesc, true, selectionObject );
  318.     }            
  319.     return anErr;
  320. }
  321.  
  322. // ************************************************************************************************
  323.  
  324. PASCAL    OSErr    AEHMakeIconSuite( const AEDescList *iconFamilyRecPtr,
  325.                                         Handle *iconSuitePtr )
  326. {
  327.  
  328.     static    DescType    iconTypes[] = {    typeIconAndMask, type8BitIcon, type4BitIcon,
  329.                                         typeSmallIconAndMask, typeSmall8BitIcon, typeSmall4BitIcon };
  330.     const    long        iconTypesCnt = 6;
  331.     
  332.     OSErr    anErr = noErr;
  333.     
  334.     AEDescList    iconList = { typeNull, nil };
  335.     
  336.     anErr = AECoerceDesc( iconFamilyRecPtr, typeAERecord, &iconList );
  337.     
  338.     if ( anErr == noErr )
  339.     {
  340.         anErr = NewIconSuite( iconSuitePtr );
  341.         
  342.         if ( anErr == noErr )
  343.         {
  344.             long    index;
  345.             AEDesc    iconDataDesc = { typeNull, nil };
  346.             
  347.             for ( index = 0; index < iconTypesCnt; index++ )
  348.             {
  349.                 anErr = AEGetKeyDesc( &iconList, iconTypes[ index ],
  350.                                       typeWildCard, &iconDataDesc );
  351.                 if ( anErr == noErr )
  352.                 {
  353.                     anErr = AddIconToSuite( iconDataDesc.dataHandle,
  354.                                             *iconSuitePtr, iconTypes[ index ] );
  355.                 }
  356.             }
  357.         }
  358.     }
  359.     AEDisposeDesc( &iconList );
  360.  
  361.     return anErr;
  362. }
  363.  
  364. // ************************************************************************************************
  365.  
  366. pascal    OSErr    MyIconAction( ResType theIconType,
  367.                               Handle *theIcon,
  368.                               void *myDataPtr)
  369. {
  370.     OSErr    anErr = noErr;
  371.     
  372.     if ( *theIcon != nil )    // only add the icon if it's really there
  373.     {
  374.         AEDescList *iconFamilyRecPtr = (AEDescList*)myDataPtr;
  375.         
  376.         anErr = AEPutKeyPtr( iconFamilyRecPtr, theIconType, theIconType,
  377.                              **theIcon, GetHandleSize( *theIcon ) );
  378.     }
  379.         
  380.     return anErr;
  381. }//end MyIconAction
  382.  
  383. // ************************************************************************************************
  384.  
  385. PASCAL    OSErr    AEHMakeIconFamilyRecord( const Handle iconSuite,
  386.                                          const IconSelectorValue iconSelector,
  387.                                                AEDescList *iconFamilyRecPtr )
  388. {
  389.     OSErr    anErr = noErr;
  390.     AEDescList    iconList = { typeNull, nil };
  391.     
  392.     static    IconActionUPP iconActionUPP;
  393.     
  394.     if ( iconActionUPP == nil )
  395.     {
  396.         iconActionUPP = NewIconActionProc( &MyIconAction );
  397.     }
  398.     
  399.     // create a record for the icon family
  400.     anErr = AECreateList( nil, 0, true, &iconList );
  401.     
  402.     if ( anErr == noErr )
  403.     {
  404.         ForEachIconDo( iconSuite, iconSelector,
  405.                        iconActionUPP, &iconList );
  406.         
  407.         anErr = AECoerceDesc( &iconList, typeIconFamily, iconFamilyRecPtr );
  408.         AEDisposeDesc( &iconList );
  409.     }
  410.  
  411.     return anErr;
  412. }
  413.  
  414.  
  415.  
  416. // ************************************************************************************************
  417.  
  418. PASCAL    OSErr    AEHGetHandlerError( const AppleEvent *reply )
  419. {
  420.     OSErr        anErr = noErr;
  421.     
  422.     DescType    actualType;
  423.     long        actualSize;
  424.     long        handlerErr;
  425.     
  426.     if ( reply->descriptorType != typeNull )    // there's a reply, so there may be an error
  427.     {
  428.         OSErr    getErrErr = noErr;
  429.         
  430.         getErrErr = AEGetParamPtr( reply, keyErrorNumber, typeLongInteger, &actualType,
  431.                                     &handlerErr, sizeof( long ), &actualSize );
  432.         
  433.         if ( getErrErr != errAEDescNotFound )    // found an errorNumber parameter
  434.         {
  435.             anErr = handlerErr;                // so return it's value
  436.         }
  437.     }
  438.     return anErr;
  439. }//end AEHGetHandlerError
  440.  
  441. // ************************************************************************************************
  442.  
  443. PASCAL    Boolean    AEHSimpleIdleFunction( EventRecord *event,
  444.                                       long *sleepTime,
  445.                                       RgnHandle *mouseRgn )
  446. {
  447. #pragma unused( event )
  448.     *sleepTime = 30;
  449.     *mouseRgn = nil;
  450.     
  451.     return ( false );
  452. }//end AEHSimpleIdleFunction
  453.  
  454. // ********************************************************
  455.  
  456. PASCAL Boolean HasAppleEvents( void )
  457. {
  458.     OSErr    anErr = noErr;
  459.     
  460.     Boolean        hasAppleEvents = false;
  461.     
  462.     if ( TrapAvailable( _Gestalt ) )
  463.     {
  464.         long    response;
  465.         
  466.         if ( Gestalt( gestaltAppleEventsAttr, &response ) == noErr )
  467.         {
  468.             hasAppleEvents = ( response & (1L << gestaltAppleEventsPresent) );
  469.         }
  470.     }
  471.     else
  472.     {
  473.         hasAppleEvents = false;
  474.     }
  475.     
  476.     return hasAppleEvents ;
  477. }//end HasAppleEvents
  478.  
  479. // ********************************************************
  480.  
  481. PASCAL Boolean FinderCallsAEProcess( void )
  482. {
  483.     OSErr    anErr = noErr;
  484.     
  485.     Boolean        finderCallsAEProcess = false;
  486.     
  487.     if ( TrapAvailable( _Gestalt ) )
  488.     {
  489.         long    response;
  490.         
  491.         if ( Gestalt( gestaltFinderAttr, &response ) == noErr )
  492.         {
  493.             finderCallsAEProcess = ( response & (1L << gestaltFinderCallsAEProcess) );
  494.         }
  495.     }
  496.     else
  497.     {
  498.         finderCallsAEProcess = false;
  499.     }
  500.     
  501.     return finderCallsAEProcess ;
  502. }//end FinderCallsAEProcess
  503.  
  504. // ********************************************************
  505.  
  506. PASCAL Boolean FinderIsOSLCompliant( void )
  507. {
  508.     OSErr    anErr = noErr;
  509.     
  510.     Boolean        finderIsOSLCompliant = false;
  511.     
  512.     if ( TrapAvailable( _Gestalt ) )
  513.     {
  514.         long    response;
  515.         
  516.         if ( Gestalt( gestaltFinderAttr, &response ) == noErr )
  517.         {
  518.             finderIsOSLCompliant = ( response & (1L << gestaltOSLCompliantFinder) );
  519.         }
  520.     }
  521.     else
  522.     {
  523.         finderIsOSLCompliant = false;
  524.     }
  525.     
  526.     return finderIsOSLCompliant ;
  527. }//end FinderIsOSLCompliant
  528.  
  529. // ********************************************************
  530.  
  531.